<template>
    <div class="shop_cart_container">
        <section class="cart_bar">
            <div class="cart_icon_num" @click="showDetailView">
                <section class="cart_icon_container" :class="{active:cart.cartList.length,move_in_cart:inCart}" ref="cart">
                    <span v-if="cart.cartList.length" class="cart_list_length">
                        {{cart.cartList.length}}
                    </span>
                    <svg class="cart_icon">
                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-icon"></use>
                    </svg>
                </section>
                <div class="cart_num">
                    <div>¥ {{cart.totalPrice}}</div>
                    <div>配送费¥{{deliveryFee}}</div>
                </div>
            </div>
            <section class="gotopay" :class="{active:minimumOrderAmount<=0}">
                <span class="gotopay_button_style" v-if="minimumOrderAmount>0">还差¥{{minimumOrderAmount}}起送</span>
                <router-link :to="{path:'/confirmOrder'}" class="gotopay_button_style" v-else>去结算</router-link>
            </section>
        </section>
        <transition name="fade">
            <div class="screen_cover" v-show="showShopCartList&&cart.cartList.length" @click="showDetailView"></div>
        </transition>
        <transition name="route-side" @after-enter="shopCartAfterEnter">
            <section class="cart_food_list" ref="cartList" v-show="showShopCartList && cart.cartList.length">
                <header class="cart_list_header">
                    <h4>购物车</h4>
                    <div @click="clearCartFn">
                        <svg>
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cart-remove"></use>
                        </svg>
                        <span class="clear_cart">清空</span>
                    </div>
                </header>
                <section class="cart_list_detail">
                    <div class="detail_container">
                        <ScrollView :click="true" ref="cartListView">
                            <ul >
                                <li v-for="item in cart.cartList" :key="item.id" class="cart_list_li">
                                    <div class="cart_list_food_name ellipsis">{{item.name}}<span v-if="item.specs">[{{item.specs}}]</span></div>
                                    <div class="cart_list_food_price">
                                        <span>￥</span>
                                        <span>{{item.price}}</span>
                                    </div>
                                    <AddCart :food="item" 
                                        :shopId="shopId"
                                        @addToCart="addToCartFn"
                                        @reduceCart="reduceCartFn">
                                    </AddCart>
                                </li>
                            </ul>
                        </ScrollView>
                    </div>
                </section>
            </section>
        </transition>
    </div>
</template>

<script>
import AddCart from './AddCart';
import ScrollView from '@src/components/common/Scroll'
import {mapActions} from 'vuex'
export default {
    components:{
        AddCart,
        ScrollView
    },
    props:{
        'geohash':String,
        'shopId':String,
        'cart':Object,
        'deliveryFee':Number,
        'minOrderAmount':Number,
        'inCart':Boolean,
    },
    computed:{
        minimumOrderAmount(){
            return this.minOrderAmount - this.cart.totalPrice
        }
    },
    data(){
        return {
            showShopCartList:false, // 显购物车列表
        }
    },
    mounted(){
        this.$nextTick(()=>{
            this.listenInCart();
        })
    },
    methods:{
        ...mapActions(['addCart','reduceCart','clearCart']),
        listenInCart(){
            this.$refs.cart.addEventListener('animationend', () => {
                this.$emit('changeInCart')
            })
            // this.$refs.cart.addEventListener('webkitAnimationEnd', () => {
            //     this.$emit('changeInCart')
            // })
        },
        shopCartAfterEnter(){
            // 刷新 better-scroll 
            this.$refs.cartListView.refresh()
        },
  
        showDetailView(){
            if(this.showShopCartList){
                this.showShopCartList=false;
            }else{
                if(this.cart.cartList.length){
                    this.showShopCartList=true
                }
            }
        },
        clearCartFn(){
            this.clearCart(this.shopId)
        },
        addToCartFn(food){
            this.addCart(food);
        },
        reduceCartFn(food){
            this.reduceCart(food)
        }
    }
}
</script>

<style lang="scss" scoped>
@import "~@styl/mixin.scss";
@keyframes cartBoundce {
    0%{
        transform: scale(1);
    }
    25%{
        transform: scale(0.8);
    }
    50%{
        transform: scale(1.1);
    }
    75%{
        transform: scale(0.9);
    }
    100%{
        transform: scale(1);
    }
}
.shop_cart_container{
    z-index: 13;
    font-family: 'Helvetica Neue',Tahoma,Arial;
    .cart_bar{
        position: relative;
        @include wh(100%,2rem);
        background: #3d3d3f;
        display: flex;
        z-index: 20;
        .cart_icon_num{
            flex: 1;
            display: flex;
            .cart_icon_container{
                background: #3d3d3f;
                position: absolute;
                top:-0.6rem;
                left: 0.5rem;
                padding: .4rem;
                border: 0.18rem solid #444;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                .cart_list_length{
                    position: absolute;
                    top: -.25rem;
                    right: -.25rem;
                    background-color: #ff461d;
                    min-width: .7rem;
                    height: .7rem;
                    line-height: .7rem;
                    border-radius: 50%;
                    text-align: center;
                    @include sc(.5rem, #fff);
                    font-family: Helvetica Neue,Tahoma,Arial;
                }
                .cart_icon{
                    @include wh(1.2rem,1.2rem)
                }
                &.active{
                    background: $blue;
                }
                &.move_in_cart{
                    animation: cartBoundce 0.5s ease-in-out;
                }
            }
            .cart_num{
                position: absolute;
                left: 3.5rem;
                color: #fff;
                div{
                    color: #fff;
                }
                div:nth-of-type(1){
                    font-size: .8rem;
                    font-weight: bold;                    
                }
                div:nth-of-type(2){
                    font-size: .4rem;
                }
            }
        }
        .gotopay{
            position: absolute;
            right: 0;
            top: 0;
            @include wh(5rem, 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            background: #535356;
            .gotopay_button_style{
                @include sc(.7rem, #fff);
                font-weight: bold;
            }
            &.active{
                background: $green;
            }
        }
    } 
    .cart_food_list{
        background: #fff;
        z-index: 12;
        position: fixed;
        left: 0;
        width: 100%;
        bottom: 2rem;
        padding-bottom: 0.5rem;
        .cart_list_header{
            padding: 0.5rem 0.5rem;
            background: #eceff1;
            @include fj;
            h4{
                @include sc(0.66rem,#333);
            }
            div{
                display: flex;
                align-items: center;
                justify-content: center;
                svg{
                    @include wh(0.6rem,0.6rem);
                }
                span{
                    @include sc(0.55rem,#444);
                }
            }
        }
        .cart_list_detail{
            max-height: 15rem;
            display:flex;
            .detail_container{
                width: 100%;
                flex: 1;
                overflow: hidden;
            }
            .cart_list_li{
                padding: 0.4rem 0.3rem 0.3rem 0.5rem;
                border-bottom: 0.025rem solid #f2f2f2;
                display: flex;
                align-items: center;
                .cart_list_food_name{
                    flex: 1;
                    @include sc(0.75rem,#333);
                    font-weight: 500;
                    span{
                        margin-left: 0.5rem;
                        @include sc(0.6rem,#666);
                    }
                }
                .cart_list_food_price{
                    font-size: 0;
                    span{
                        color: #ff461d;
                    }
                    span:nth-of-type(1){
                        font-size: 0.5rem;
                    }
                    span:nth-of-type(2){
                        font-size: 0.7rem;
                        font-weight: 700;
                        margin-right: 1.5rem;
                    }
                }
            }
        }

    }
    .screen_cover{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0,0,0,.3);
        z-index: 11;
    }
}
.route-side-enter,.route-side-leave-to{
    transform: translateY(100%);

}
.route-side-enter-active,.route-side-leave-active{
    transition: all 0.3s ease-out;
}
.fade-enter,.fade-leave-to{
    opacity: 0;
}
.fade-enter-active,.fade-leave-active{
    transition: opacity 0.3s
}

</style>